package com.menggudf.service.impl;

import com.menggudf.common.utils.DateUtils;
import com.menggudf.entity.User;
import com.menggudf.mapper.UserDao;
import com.menggudf.model.ResultModel;
import com.menggudf.model.UserModel;
import com.menggudf.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.*;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2025-02-20 16:58:34
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<User> queryByPage(User user, PageRequest pageRequest) {
        long total = this.userDao.count(user);
        return new PageImpl<>(this.userDao.queryAllByLimit(user, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }

    @Override
    public void insert(String string, int i) {

    }

    @Override
    public ResultModel userRegister(UserModel user) {
        return null;
    }

    @Override
    public ResultModel userLogin(String phone, String passWord) {
        return null;
    }

    @Override

    public Map<String, Object> getUserList(String userCode, String userName, int pageSize, int offset, Date startTime, Date endTime) {
        //List<User> list = userDao.getUserList(userCode,userName,pageSize,offset,startTime, endTime);
        //long count = userDao.getUserListCount(userCode, userName, startTime, endTime);
        List<User> list = new ArrayList<>();
        long count = 1;
        List<UserModel> mlist = new ArrayList<UserModel>();
        for(User u :list){
            UserModel um = new UserModel();
            um.setBirth(u.getBirth());
            um.setDay(DateUtils.dateGap(new Date(), u.getBirth()));
            um.setCreateTime(u.getCreateTime());
            um.setPhone(u.getPhone());
            um.setRealName(u.getRealName());
            um.setSex(u.getSex());
            um.setUserCode(u.getUserCode());
            um.setUserName(u.getUserName());
            mlist.add(um);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    public ResultModel getRoles(String primaryRole) {
        return null;
    }

    @Override
    public ResultModel currRole(String currRole, Integer isMainRole) {
        return null;
    }

    @Override
    public ResultModel editUser(String currRole, Integer isMainRole, String name, Integer sex, Date birth) {
        return null;
    }

    @Override
    public ResultModel findUserPsw(String phone, String password) {
        return null;
    }

    @Override
    public ResultModel createRole(String name, Integer sex, Date birth, String userCode) {
        return null;
    }

    @Override
    public UserModel getUserDetail(String userCode) {
        return null;
    }

    @Override
    public ResultModel addBMIRecord(String userCode, String roleCode, Double bodyWeight, Double height) {
        return null;
    }

    @Override
    public ResultModel getBMIRecord(String userCode, String roleCode) {
        return null;
    }
}
